package com.example.scity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.scity.beens.GuangGaoBeen;
import com.google.gson.Gson;
import com.youth.banner.Banner;
import com.youth.banner.loader.ImageLoader;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    Banner banner;
    Button button;
    TextView tiaoguo;
    List<String> imgList = new ArrayList<>();
    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            banner = findViewById(R.id.banner);
            banner.setImages(imgList);
            banner.setImageLoader(new ImageLoader() {
                @Override
                public void displayImage(Context context, Object o, ImageView imageView) {
                    Glide.with(context).load(o).into(imageView);
                }
            });
            banner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    if(position == imgList.size()-1){
                        button.setVisibility(View.VISIBLE);
                        banner.setDelayTime(2000000);
                        banner.stopAutoPlay();
                    }else{
                        button.setVisibility(View.GONE);
                    }
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
            banner.setDelayTime(2000);
            banner.start();
        }
    };


    void toMain2(){
        Intent i = new Intent(this,MainActivity2.class);
        startActivity(i);//启动i里面的活动，MainActivity2
        finish();//关闭本活动
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Configs.ReadIp(this);   //程序启动去配置
        if(Configs.LoadTimes(this)){
            toMain2();
        }
        button = findViewById(R.id.button2);
        tiaoguo = findViewById(R.id.textView2);
        tiaoguo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toMain2();
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
                alertDialog.setTitle("网络设置");
                View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.netset_dialog,null);
                EditText ip = view.findViewById(R.id.net_ip);
                EditText port = view.findViewById(R.id.net_port);
                alertDialog.setView(view);
                ip.setText(Configs.mIp);
                port.setText(Configs.mPort);
                alertDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                alertDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String sip = ip.getText().toString();
                        String sp = port.getText().toString();
                        if(sip.trim().equals("")||sip.contains(" ")){
                            Toast.makeText(MainActivity.this,"请正确输入IP",Toast.LENGTH_LONG).show();
                            return;
                        }
                        Configs.WriteIp(MainActivity.this,ip.getText().toString(),port.getText().toString());
                        toMain2();
                    }
                });
                alertDialog.show();
            }
        });

        try {
            getImageList();
        }finally {

        }

    }

    void getImageList(){
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(Configs.Host + APIInterface.GuangGao+"?type=2").build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String json = response.body().string();
                GuangGaoBeen guangGaoBeen = new Gson().fromJson(json,GuangGaoBeen.class);
                for(int i=0;i<guangGaoBeen.rows.size();i++){
                    imgList.add(Configs.Host+guangGaoBeen.rows.get(i).advImg);
                }
                handler.sendEmptyMessage(1);
            }
        });
    }
}